<?php
/**
 * Date: 2017/11/7
 * Time: 18:19
 * Author: zeronlee
 */
namespace App\Http\Controllers\Backend\Access;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\Access\User\User;
use App\Models\Access\Role\Role;
use App\Models\Access\Permission\Permission;

class RoleController extends Controller
{
    protected $role;

    public function __construct () {
        $this->role = new Role();
    }

    /**
     * 用户首页
     */
    public function index (){

        //模板
        return view('backend.access.roleindex');
    }

    /**
     * 异步查询
     */
    public function search(Request $request){
        $param = $request->input();

        //搜索
        $search = $param['search'];
        //记录数
        $count = Role::getCount($search['value']);

        //列表
        $roles = $this->role->getList($search['value']);

        $data = array('draw'=>$param['draw'],'recordsTotal'=>$count,'recordsFiltered'=>$count,'data'=>$roles);
        return response()->json($data);
    }
    /**
     * 存储用户信息
     */
    public function add (Request $request,Permission $permission){
        if ($request->isMethod('Post')){

            $validator = Role::validate($request->all());
            if ($validator->fails()) {
                return redirect('admin/roleAdd')
                    ->withErrors($validator)
                    ->withInput();
            }
            if ($this->role->insertRow($request)){

                //跳转列表
                return redirect('admin/roleIndex');
            } else {
                return redirect('admin/roleAdd')
                    ->withErrors($validator)
                    ->withInput();
            }
        }else{
            $fields = ['id','name','code'];
            return view('backend.access.roleadd')
                ->with(['action'=>'add'])
                ->with(['permissions'=>$permission->getList($fields,[0,$permission->getCount()])]);
        }

    }

    /**
     * 更新用户信息
     */
    public function update (Request $request,Permission $permission){
        if ($request->isMethod('post')){
            $validator = Role::validate($request->all(),'update');
            if ($validator->fails()) {
                return redirect('admin/roleUpdate/id/' . $request->id)
                    ->withErrors($validator)
                    ->withInput();
            }
            if($this->role->updateRow($request)){
                //跳转列表
                return redirect('admin/roleIndex');
            } else {
                //显示错误
                return redirect('admin/roleUpdate/id/' . $request->id)
                    ->withErrors($validator)
                    ->withInput();
            }
        } else {
            $item = $this->role->getRoles($request->id);
            $fields = ['id','name','code'];
            $permissions = $permission->getList($fields,[0,$permission->getCount()]);
            return view('backend.access.roleadd')
                ->with(['permissions'=>$permissions])
                ->with(['action'=>'update'])
                ->with(['item'=>$item]);
        }

    }

    /**
     * 删除
     */
    public function delete (Request $request){
        $data['code'] = 0;
        try{
            $this->role->deleteRow($request);

        }catch (\Exception $e){
            $data['code'] = 403;//非法操作

            $data['msg'] = $e->getMessage();

        }
        return response()->json($data);
    }

}